package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.FileShare;

import com.kaishengit.util.DBhelp;
import com.kaishengit.util.Pager;
import com.kaishengit.util.RowMapper;

public class FileShareDao {

	DBhelp<FileShare> help = new DBhelp<FileShare>();
	
	public void insert(FileShare fs){
		String sql = "insert into  fileshare(fs_id,fs_name,fs_createTime,pid,mid)values(?,?,?,?,?)";
		help.executeSQL(sql, fs.getFs_id(),fs.getFs_name(),fs.getFs_createTime(),fs.getMid(),fs.getPid());
	}
	private int getTotalCounts(String pid){
		String sql = "select count(*) from fileshare where pid = ?";
		return help.executeQueryForCount(sql,pid);
	}
	public Pager findAll(String pid,int num) {
		String sql = "select fs_id,fs_name,fs_createTime,pid,mid from fileshare where pid = ? limit ?,?";
		Pager pager = new Pager(getTotalCounts(pid),5,num);
		List list = help.executeForList(sql, new FileShareRowMapper(), pid,pager.getStartIndex(),pager.getPerSize());
		pager.setResult(list);
		return pager;
	}
	private class FileShareRowMapper implements RowMapper<FileShare>{

		public FileShare mapper(ResultSet rs) throws SQLException {
			FileShare fs = new FileShare();
			fs.setFs_id(rs.getString("fs_id"));
			fs.setFs_name(rs.getString("fs_name"));
			fs.setFs_createTime(rs.getString("fs_createTime"));
			fs.setMid(rs.getString("mid"));
			fs.setPid(rs.getString("pid"));
			return fs;
		}
		
		
	}
	public FileShare findById(String fsid) {
		String sql = "select fs_id,fs_name,fs_createTime,pid,mid from fileshare where fs_id = ?";
		return help.executeQuery(sql, new FileShareRowMapper(), fsid);
	}

	




}
